﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PatientManagement.Managers;

namespace PatientManagement
{
    public partial class FormTemplateManagement : Form
    {
        public FormTemplateManagement()
        {
            InitializeComponent();
            dtgTemplate.AutoGenerateColumns = false;
            GetTemplates();
        }

        public void GetTemplates()
        {
            var templates = ServiceManager.Instance.GetPrescriptionTemplates();
            dtgTemplate.DataSource = templates;
        }

        private void dtgTemplate_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == dtgTemplate.Columns["colViewDetails"].Index)
                {
                    int prescriptionId = Convert.ToInt32(dtgTemplate["colId", e.RowIndex].Value);
                    FormAddTemplate form = new FormAddTemplate(prescriptionId);
                    form.ShowDialog();
                    if (form.IsUpdated)
                    {
                        GetTemplates();
                    }
                }
                else if (e.ColumnIndex == dtgTemplate.Columns["colDelete"].Index)
                {
                    int prescriptionId = Convert.ToInt32(dtgTemplate["colId", e.RowIndex].Value);
                    var dialogResult = MessageBox.Show("Bạn có muốn xóa không", "Xác nhận", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        var result = ServiceManager.Instance.DeleteTemplate(prescriptionId);
                        if (result)
                        {
                            GetTemplates();
                        }
                        else
                        {
                            MessageBox.Show("Xóa thất bại");
                        }
                    }
                }
            }
        }

        private void btnAddTemplate_Click(object sender, EventArgs e)
        {
            FormAddTemplate form = new FormAddTemplate();
            form.ShowDialog();
            GetTemplates();
        }



    }
}
